/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.foxline.clinicplus.service;

import br.com.foxline.clinicplus.entity.Appraisal;
import br.com.foxline.clinicplus.entity.Customer;
import br.com.foxline.clinicplus.entity.Diary;
import br.com.foxline.clinicplus.entity.Doctor;
import br.com.foxline.clinicplus.enums.Reports;
import br.com.foxline.clinicplus.enums.SituationAppraisal;
import br.com.foxline.clinicplus.enums.TypeProcedure;
import java.util.*;
import javax.ejb.EJB;
import javax.ejb.Stateless;
import javax.persistence.Query;

/**
 *
 * @author roniere
 */
@Stateless
public class AppraisalService extends GenericService<Appraisal> {

    @EJB
    ClinicService clinicService;

    public AppraisalService() {
        super(Appraisal.class);
    }

    public int getAge(Date data) {
        int idade = 0;
        if (data != null) {
            Calendar c = new GregorianCalendar();
            Calendar dateBirth = new GregorianCalendar();
            dateBirth.setTime(data);

            idade = c.get(Calendar.YEAR) - dateBirth.get(Calendar.YEAR);

            dateBirth.add(Calendar.YEAR, idade);

            if (c.before(dateBirth)) {
                idade--;
            }
        }
        return idade;

    }

    public void genReport(String text, Appraisal a) {
        HashMap<String, Object> parametros;
        parametros = new HashMap<String, Object>();
        parametros.put("TEXT", text);

        try {
            parametros.put("idade", getAge(a.getCustomer().getDateBirth()));
        } catch (Exception e) {
        }

        List<Appraisal> l = new ArrayList<Appraisal>();
        l.add(a);

        parametros = clinicService.getCabecalho(parametros);
        ReportService.genReport(parametros, l, Reports.APPRAISAL);
    }

    public List<Diary> procedureAppraisal(TypeProcedure typeProcedure, SituationAppraisal situationAppraisal) {

        String sql = "Select d from Appraisal a JOIN a.diary d "
                + " JOIN d.procedure p where ";

        if (typeProcedure != null) {
            sql += " p.typeProcedure = :typeProcedure and";
        }

        if (situationAppraisal != null) {
            sql += " a.situationAppraisal = :situationAppraisal";
        }

        Query createQuery = getEntityManager().createQuery(sql);

        if (typeProcedure != null) {
            createQuery.setParameter("typeProcedure", TypeProcedure.EXAME);
        }
        if (situationAppraisal != null) {
            createQuery.setParameter("situationAppraisal", SituationAppraisal.WAITING);
        }
        System.err.println(sql);
        return createQuery.getResultList();
    }

    public List<Appraisal> appraisalSituationAppraisal(SituationAppraisal situationAppraisal) {

        String sql = "Select a from Appraisal a WHERE a.situationAppraisal = :situationAppraisal";


        Query createQuery = getEntityManager().createQuery(sql);
        createQuery.setParameter("situationAppraisal", situationAppraisal);

        return createQuery.getResultList();
    }

    public List<Appraisal> searchAppraisal(Long codLaudo, Long codAtendimento, String nome, SituationAppraisal situationAppraisal,
            Date data, Doctor doctor, Customer... customer) {

        System.err.println(data);
        String sql = "Select a from Appraisal a ";

        if (codAtendimento != null || (nome != null && !nome.isEmpty()) || data != null) {
            sql += " JOIN a.diary d ";
        }
        if (nome != null && !nome.isEmpty()) {
            sql += " JOIN d.customer c ";
        }
        
        if(doctor != null){
            sql += " JOIN a.doctor dc ";
        }
        
        try {
            if (codAtendimento == null && (nome == null || nome.isEmpty())) {
                sql += " JOIN a.diary d ";
            }
            if (customer[0] != null && (nome == null || nome.isEmpty())) {
                sql += " JOIN d.customer c ";
            }
        } catch (Exception e) {
        }

        sql += " WHERE ";

        try {
            if (customer[0] != null && (nome == null || nome.isEmpty())) {
                sql += " c.id = :customer and ";
            }
        } catch (Exception e) {
        }

        if(doctor != null){
            sql += " dc.id = :doctor";
        }
        if (data != null) {
            sql += " d.startTime BETWEEN :data AND :dataf and ";
        }

        if (codLaudo != null) {
            sql += " a.id = :codLaudo and ";
        }

        if (codAtendimento != null) {
            sql += " d.id = :codAtendimento and ";
        }

        if (nome != null && !nome.isEmpty()) {
            sql += " lower(c.name) like lower(:name) and ";
        }

        if (situationAppraisal != null) {
            sql += " a.situationAppraisal = :situationAppraisal and ";
        }

        sql += " 1 = 1 ";
        Query createQuery = getEntityManager().createQuery(sql);

        try {
            if (customer[0] != null) {
                createQuery.setParameter("customer", customer[0].getId());
            }
        } catch (Exception e) {
        }


        if (data != null) {
            
            Calendar gc1 = GregorianCalendar.getInstance();
            gc1.setTime(data);

            if (gc1.get(GregorianCalendar.HOUR_OF_DAY) == 0) {
                gc1.set(GregorianCalendar.HOUR_OF_DAY, 0);
            }

            Calendar gc2 = GregorianCalendar.getInstance();
            gc2.setTime(data);
            if (gc2.get(GregorianCalendar.HOUR_OF_DAY) == 0) {
                gc2.set(GregorianCalendar.HOUR_OF_DAY, 23);
                gc2.set(GregorianCalendar.MINUTE, 59);
                gc2.set(GregorianCalendar.SECOND, 0);
            }
            
            createQuery.setParameter("data", gc1.getTime());
            createQuery.setParameter("dataf", gc2.getTime());
        }
        if (codLaudo != null) {
            createQuery.setParameter("codLaudo", codLaudo);
        }
        if (codAtendimento != null) {
            createQuery.setParameter("codAtendimento", codAtendimento);
        }
        if (nome != null && !nome.isEmpty()) {
            createQuery.setParameter("name", "%" + nome + "%");
        }
        if (situationAppraisal != null) {
            createQuery.setParameter("situationAppraisal", situationAppraisal);
        }
        if (doctor != null) {
            createQuery.setParameter("doctor", doctor.getId());
        }

        return createQuery.getResultList();
    }
}
